#!/bin/bash

##############################################
# STM32 USB Device Library Download Script
##############################################

set -e

# Configuration
USB_VERSION="v1.11.0"
CUBE_H7_URL="https://github.com/STMicroelectronics/STM32CubeH7/archive/refs/tags/${USB_VERSION}.tar.gz"
TEMP_DIR="/tmp/stm32_usb_download"
TARGET_DIR="Middlewares/ST/STM32_USB_Device_Library"

echo "=========================================="
echo "STM32 USB Device Library Download Script"
echo "=========================================="
echo "Version: ${USB_VERSION}"
echo "Target: $(pwd)/${TARGET_DIR}"
echo ""

# Create directories
echo "[1/6] Creating directories..."
mkdir -p "${TARGET_DIR}/Core/Inc"
mkdir -p "${TARGET_DIR}/Core/Src"
mkdir -p "${TARGET_DIR}/Class/CDC/Inc"
mkdir -p "${TARGET_DIR}/Class/CDC/Src"
mkdir -p "${TEMP_DIR}"

# Download STM32CubeH7
echo "[2/6] Downloading STM32CubeH7 ${USB_VERSION}..."
if [ -f "${TEMP_DIR}/stm32cubeh7.tar.gz" ]; then
    echo "  - Using cached download"
else
    if command -v curl &> /dev/null; then
        curl -L "${CUBE_H7_URL}" -o "${TEMP_DIR}/stm32cubeh7.tar.gz"
    elif command -v wget &> /dev/null; then
        wget "${CUBE_H7_URL}" -O "${TEMP_DIR}/stm32cubeh7.tar.gz"
    else
        echo "Error: Neither curl nor wget found. Please install one of them."
        exit 1
    fi
    echo "  - Download complete"
fi

# Extract
echo "[3/6] Extracting STM32CubeH7..."
cd "${TEMP_DIR}"
tar -xzf stm32cubeh7.tar.gz
cd -

# Copy USB Device Core files
echo "[4/6] Copying USB Device Core files..."
CUBE_USB_PATH="${TEMP_DIR}/STM32CubeH7-${USB_VERSION}/Middlewares/ST/STM32_USB_Device_Library"

if [ -d "${CUBE_USB_PATH}" ]; then
    # Core source files
    echo "  - Copying Core source files..."
    cp "${CUBE_USB_PATH}/Core/Src/usbd_core.c" "${TARGET_DIR}/Core/Src/"
    cp "${CUBE_USB_PATH}/Core/Src/usbd_ctlreq.c" "${TARGET_DIR}/Core/Src/"
    cp "${CUBE_USB_PATH}/Core/Src/usbd_ioreq.c" "${TARGET_DIR}/Core/Src/"
    
    # Core header files
    echo "  - Copying Core header files..."
    cp "${CUBE_USB_PATH}/Core/Inc/usbd_core.h" "${TARGET_DIR}/Core/Inc/"
    cp "${CUBE_USB_PATH}/Core/Inc/usbd_ctlreq.h" "${TARGET_DIR}/Core/Inc/"
    cp "${CUBE_USB_PATH}/Core/Inc/usbd_ioreq.h" "${TARGET_DIR}/Core/Inc/"
    cp "${CUBE_USB_PATH}/Core/Inc/usbd_def.h" "${TARGET_DIR}/Core/Inc/"
else
    echo "Error: USB Device Library not found in STM32CubeH7"
    exit 1
fi

# Copy USB CDC Class files
echo "[5/6] Copying USB CDC Class files..."
if [ -d "${CUBE_USB_PATH}/Class/CDC" ]; then
    # CDC source files
    echo "  - Copying CDC source files..."
    cp "${CUBE_USB_PATH}/Class/CDC/Src/usbd_cdc.c" "${TARGET_DIR}/Class/CDC/Src/"
    
    # CDC header files
    echo "  - Copying CDC header files..."
    cp "${CUBE_USB_PATH}/Class/CDC/Inc/usbd_cdc.h" "${TARGET_DIR}/Class/CDC/Inc/"
else
    echo "Error: USB CDC Class not found in STM32CubeH7"
    exit 1
fi

# Create directory structure info
echo "[6/6] Creating directory structure..."
mkdir -p "${TARGET_DIR}"

# Cleanup
echo ""
echo "Cleaning up temporary files..."
# Keep the download for future use
# rm -rf "${TEMP_DIR}"

echo ""
echo "=========================================="
echo "USB Device Library Setup Complete!"
echo "=========================================="
echo ""
echo "Files installed to: $(pwd)/${TARGET_DIR}"
echo ""
echo "Next steps:"
echo "  1. Create USB configuration files (usbd_conf.c/h)"
echo "  2. Create USB descriptor file (usbd_desc.c/h)"
echo "  3. Create CDC interface file (usbd_cdc_if.c/h)"
echo "  4. Update Makefile to include USB library"
echo "  5. Build the project: make clean && make all"
echo ""

# Show installed files
echo "USB Device Library files:"
ls -lh "${TARGET_DIR}/Core/Src/"
echo ""
ls -lh "${TARGET_DIR}/Class/CDC/Src/"

